import axios from 'axios'

export default class Common {
  svrurl = ''
  codelist =[]
  // 构造
  constructor (url) {
    this.svrurl = url
    var path = this.svrurl + '/article/test/getMcode'
    axios.get(path).then(response => {
      this.codelist = response.data.mcodelist
      // console.log(this.codelist)
    })
  }

  // MCode显示文本
  getMCode (cls, code) {
    var lst = this.getMList(cls)
    let len = lst.length
    for (let i = 0; i < len; i++) {
      if (lst[i].classcd.toString() === cls && lst[i].itemvalue.toString() === code) {
        return lst[i].itemcontent
      }
    }
    return 'no value'
  }

  // MCode列表
  getMList (cls) {
    return this.codelist.filter((item, index) => { return (item.classcd.toString() === cls.toString()) })
  }

  // get后台
  getservice (path) {
    return new Promise((resolve, reject) => {
      var url = this.svrurl + path
      axios.get(url).then(response => {
        resolve(response.data)
      })
    })
  }

  // Post后台
  postservice (path, param) {
    return new Promise((resolve, reject) => {
      var url = this.svrurl + path
      axios.post(url, param).then(response => {
        resolve(response.data)
      })
    })
  }

  // 取得申请信息
  getApplyInfo (applycd) {
    return this.getservice('/article/test/getApplyById?id=' + applycd)
  }

  // 经销商列表
  getDealerList (userid) {
    return this.getservice('/article/test/getDealerList?userid=' + userid)
  }

  // 金融机构列表
  getFininstList (dealerid) {
    return this.getservice('/article/test/getFininstList?dealerid=' + dealerid)
  }

  // 产品列表
  getProductList (fininstid) {
    return this.getservice('/article/test/getProductList?fininstid=' + fininstid)
  }

  // 查询申请
  searchApply (param) {
    return this.postservice('/article/test/searchApply', param)
  }
}
